import java.io.*;
import javax.swing.*;
import java.util.*;
import java.awt.*;

/**
 * Displays and updates high scores
 * 
 * @author  James Wilkinson <jhwilkin@purdue.edu>
 * @version 1.7
 * @since   2011-11-21
 **/
public class HighScores
{
  
    public Frame myFrame;
    
    private String names[];    
    private int scores[];
    
    public HighScores()
    {
      
      scores = new int[Settings.numScores];
      names = new String[Settings.numScores];
      
      File highScoresIn = new File(Settings.highScoresFileName);
      
      //Check if the file named in Settings.highScoresFileName exists.
      if(highScoresIn.exists()){
          
        Scanner s = null;
        try{
          s = new Scanner(highScoresIn);
        }catch(Exception e){}
      
        for(int i = 0; i < Settings.numScores; i++){
          if(s.hasNext()){
              
            String temp = s.nextLine();
            temp = temp.replace("\t", " ");
            
            String tempArray[] = temp.split(" ");
            scores[i] = Integer.parseInt(tempArray[0]);
            names[i] = tempArray[1];
            
          }else{
           scores[i] = 0;
           names[i] = "nobody";
          }
        }
      }else{ 
        
        PrintWriter p = null;
        
        try{
          highScoresIn.createNewFile();
          p = new PrintWriter(highScoresIn);
        }catch(Exception e){}
          
        for(int i = 0; i<Settings.numScores;i++){
           
          names[i] = "nobody";
          scores[i] = 0;
          p.print(scores[i] + " " + names[i] + "\n");
        }
          
        p.close();
     }
   }
    
    /**
     * Display high scores
     * 
     * @param name  Name of player to highlight
     * @param score Score of player to highlight
     **/
    public void showHighScores(String name, int score)
    {
      
      //Display high scores and if an entry has the same name and
      //score as the passed parameters, highilght it with >>> <<<
      String temp = "";
      
      for(int i = 0; i < Settings.numScores; i++){
        if(names[i].equals(name) && scores[i] == score){
          temp+= ">>>" + names[i] + "-" + scores[i] + "<<<" + "\n";
          
        }else{
          temp+= names[i] + "-" + scores[i] + "\n";
        }
      }
      
      JOptionPane.showMessageDialog(null, temp , "HighScores", JOptionPane.INFORMATION_MESSAGE);
    }
    
    /**
     * Add score to high scores if greater than lowest high score
     * 
     * @param score Score to try to add
     **/
    public void addHighScore(String name, int score)
    {
      
      //If the provided score is greater than the lowest high score,
      //then replace the lowest score with the new score and then
      //call the sortScores() method.
      int min = 1000000000;
      
      for(int i = 0; i < Settings.numScores; i++){
        if(scores[i] < min){
          
          min = scores[i];
          
          if(score > min){
            scores[scores.length - 1] = score;
            names[scores.length - 1] = name;
          }
        }
      }
      
      sortScores();
    }
    
    /**
     * Sort bottom score up to correct position in high scores table
     **/
    public void sortScores()
    {
      
      //Use a single round of bubble sort to bubble the last entry
      //in the high scores up to the correct location.
      for(int pass = 1; pass < scores.length; pass++){
        for(int i = 0; i < scores.length-pass; i++){
         
          if(scores[i] < scores[i+1]){
           
            int temp = scores[i];
            scores[i] = scores[i+1];
            scores[i+1] = temp;
            
            String temp2 = names[i];
            names[i] = names[i+1];
            names[i+1] = temp2;
          }
        }
      }
    }
    
    /**
     * Write scores to high scores file
     **/
    public void writeScores()
    {
      
      //Write the high scores data to the file name indicated
      //by Settings.highScoresFileName.
      PrintWriter p = null;
      
      try{
        p = new PrintWriter(Settings.highScoresFileName);
      }catch(Exception e){}
      
      for(int i = 0; i < Settings.numScores; i++){
        
        System.out.print(scores[i] + " " + names[i] + "\n");
        p.print(scores[i] + " " + names[i] + "\n");
      }
      
      p.close();
    }
}